package guava.study.lyh.other;

import com.google.common.base.CharMatcher;
import com.google.common.base.Preconditions;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.MapDifference;
import com.google.common.collect.Maps;
import com.google.common.primitives.Ints;
import org.junit.Test;

import java.util.Date;
import java.util.Map;

/**
 * 作者 yaohua.liu
 * 日期 2014-08-05 16:18
 * 包名 com.qunar.fresh.lyh
 * 说明 ...
 */
public class Guava基本数据类型 {
    @Test
    public void testCompare(){
        Preconditions.checkState("89983".equals(CharMatcher.DIGIT.retainFrom("some text 89983 and more")));
    }

    @Test
    public void testBiMap(){
        System.out.println(TestBiMap.getElementNumber("Helium"));
        System.out.println(TestBiMap.getElementName(1));
    }

    /**
     * 双向map
     */
     static class TestBiMap{
        private static final BiMap<Integer,String> NUMBER_TO_NAME_BIMAP;

        static {
            NUMBER_TO_NAME_BIMAP = HashBiMap.create();
            NUMBER_TO_NAME_BIMAP.put(1, "Hydrogen");
            NUMBER_TO_NAME_BIMAP.put(2, "Helium");
            NUMBER_TO_NAME_BIMAP.put(3, "Lithium");
        }

        public static int getElementNumber(String elementName) {
            return NUMBER_TO_NAME_BIMAP.inverse().get(elementName);
        }

        public static String getElementName(int elementNumber) {
            return NUMBER_TO_NAME_BIMAP.get(elementNumber);
        }
    }
    /**
     * 不允许为空值的Set
     */
    @Test
    public void testSets() {
/*        Set<String> set = Sets.newHashSet();
        Set<String> constrainedSet = Constraints.constrainedSet(set, Constraints.notNull());
        constrainedSet.add("A");
        constrainedSet.add(null); // NullPointerException here*/
    }

    /**
     * 求map的并，交，差集
     */
    @Test
    public void testMap(){
        Map<Object, Object> mapA = Maps.newHashMap();
        Map<Integer,Object> mapB = Maps.newHashMap();
        mapA.put(1,"a");
        mapA.put(2,"b");
        mapA.put(4,"22");

        mapB.put(1,"a");
        mapB.put(3,"c");
        mapB.put(4,"c");

        MapDifference differenceMap = Maps.difference(mapA, mapB);
        Map entriesDiffering = differenceMap.entriesDiffering();//Returns an unmodifiable map describing keys that appear in both maps, but with different values.
        System.out.println("1:"+entriesDiffering.toString());
        Map entriesOnlyOnLeft = differenceMap.entriesOnlyOnLeft();//Only On Left
        System.out.println("2:"+entriesOnlyOnLeft.toString());
        Map entriesOnlyOnRight = differenceMap.entriesOnlyOnRight();//Only On Right
        System.out.println("3:"+entriesOnlyOnRight.toString());
        Map entriesInCommon = differenceMap.entriesInCommon();//Returns an unmodifiable map containing the entries that appear in both maps; that is, the intersection of the two maps.
        System.out.println("4:"+entriesInCommon.toString());
    }

    /**
     * 基本类型int的用法
     */
    @Test
    public void testInts(){
        int[] array = { 1, 2, 3, 4, 5 };
        int a = 4;
        boolean contains = Ints.contains(array, a);
        System.out.println(contains);

        int indexOf = Ints.indexOf(array, a);
        System.out.println(indexOf);

        int max = Ints.max(array);
        int min = Ints.min(array);
        System.out.println(max+":"+min);

        //        int[] concat = Ints.concat(array, array2);
    }

    @Test
    public void testPreconditions() {
        new PostExample("aa", null, null);
    }

    class PostExample {
        String title;
        Date date;
        String author;

        public PostExample(final String title, final Date date, final String author) {
            Preconditions.checkNotNull(title);
            Preconditions.checkNotNull(date, "date can't null");
            Preconditions.checkNotNull(author);
            this.title = title;
            this.date =date;
            this.author = author;
        }
    }
}
